package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.sumpu.app.guoguojie.dao.DoingTagDao;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.DoingProduct;
import cn.sumpu.app.guoguojie.domain.DoingSystemTag;
import cn.sumpu.app.guoguojie.domain.DoingTag;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class DoingTagDaoImpl extends SqlSessionDaoSupport implements
		DoingTagDao {

	// 添加标签信息
	@Override
	public void addDoingTag(DoingTag doingTag) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.DoingTagMapper.addDoingTag",
							doingTag);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addDoingTag database failure.", e);
			}
			throw new AppBusinessException("addDoingTag database failure.", e);
		}
	}

	// 删除标签信息
	@Override
	public void deleteDoingTagById(String tagId) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.DoingTagMapper.deleteDoingTagById",
							tagId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteDoingTagById database failure.", e);
			}
			throw new AppBusinessException(
					"deleteDoingTagById database failure.", e);
		}
	}

	// 删除标签(包括子类)
	@Override
	public void deleteDoingTagAll(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.DoingTagMapper.deleteDoingTagAll",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteDoingTagAll database failure.", e);
			}
			throw new AppBusinessException(
					"deleteDoingTagAll database failure.", e);
		}
	}

	// 更新标签信息
	@Override
	public void updateDoingTagById(DoingTag doingTag)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.DoingTagMapper.updateDoingTagById",
							doingTag);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateDoingTagById database failure.", e);
			}
			throw new AppBusinessException(
					"updateDoingTagById database failure.", e);
		}
	}

	// 取得标签列表
	@SuppressWarnings("unchecked")
	@Override
	public List<DoingTag> getDoingTagList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<DoingTag>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.getDoingTagList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingTagList database failure.", e);
			}
			throw new AppBusinessException("getDoingTagList database failure.",
					e);
		}
	}

	// 取得所有标签
	@SuppressWarnings("unchecked")
	@Override
	public List<DoingTag> getDoingTagAll(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<DoingTag>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.getDoingTagAll",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingTagAll database failure.", e);
			}
			throw new AppBusinessException("getDoingTagAll database failure.",
					e);
		}
	}

	// 取得前台标签
	@SuppressWarnings("unchecked")
	@Override
	public List<DoingTag> getDoingTagShow(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<DoingTag>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.getDoingTagShow",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingTagShow database failure.", e);
			}
			throw new AppBusinessException("getDoingTagShow database failure.",
					e);
		}
	}

	// 取得所有标签名称
	@SuppressWarnings("unchecked")
	@Override
	public List<DoingTag> getDoingTagName() throws AppBusinessException {
		try {
			return (List<DoingTag>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.getDoingTagName");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingTagName database failure.", e);
			}
			throw new AppBusinessException("getDoingTagName database failure.",
					e);
		}
	}

	// 根据ID取得标签及所有上级标签
	@Override
	public DoingTag getDoingTagSelfAndParent(String identifier)
			throws AppBusinessException {
		try {
			return (DoingTag) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.getDoingTagSelfAndParent",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingTagSelfAndParent database failure.", e);
			}
			throw new AppBusinessException(
					"getDoingTagSelfAndParent database failure.", e);
		}
	}

	// 添加微博关联的标签
	@Override
	public void addDoingSystemTag(DoingSystemTag doingSystemTag)
			throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.DoingTagMapper.addDoingSystemTag",
							doingSystemTag);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addDoingSystemTag database failure.", e);
			}
			throw new AppBusinessException(
					"addDoingSystemTag database failure.", e);
		}
	}

	// 查询微博订正标签
	@SuppressWarnings("unchecked")
	@Override
	public List<Doing> getDoing(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<Doing>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.getDoing",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoing database failure.", e);
			}
			throw new AppBusinessException("getDoing database failure.", e);
		}
	}

	// 微博表中的总记录条数
	@Override
	public int getDoingCount() throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.getDoingCount");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingCount database failure.", e);
			}
			throw new AppBusinessException("getDoingCount database failure.", e);
		}
	}

	//根据标签取得当天分享的微博
	@SuppressWarnings("unchecked")
	@Override
	public List<Doing> getTodayDoingByTagId(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<Doing>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DoingMapper.getTodayDoingByTagId",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTodayDoingByTagId database failure.", e);
			}
			throw new AppBusinessException(
					"getTodayDoingByTagId database failure.", e);
		}
	}
	//根据标签取得当天分享的微博总记录数
	@Override
	public int getTodayDoingCountByTagId(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.DoingMapper.getTodayDoingCountByTagId",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTodayDoingCountByTagId database failure.", e);
			}
			throw new AppBusinessException(
					"getTodayDoingCountByTagId database failure.", e);
		}
	}
	//订正标签ID
	@SuppressWarnings("unchecked")
	@Override
	public List<DoingTag> getDts(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<DoingTag>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.getDts",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDts database failure.", e);
			}
			throw new AppBusinessException(
					"getDts database failure.", e);
		}
	}

	@Override
	public int getDtCount() throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.guoguojie.persistence.DoingTagMapper.getDtCount");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDtCount database failure.", e);
			}
			throw new AppBusinessException(
					"getDtCount database failure.", e);
		}
	}

	@Override
	public void updateDtId(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.updateDtId",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateDtId database failure.", e);
			}
			throw new AppBusinessException(
					"updateDtId database failure.", e);
		}
	}

	@Override
	public void updateDtPid(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update(
							"cn.sumpu.app.guoguojie.persistence.DoingTagMapper.updateDtPid",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateDtPid database failure.", e);
			}
			throw new AppBusinessException(
					"updateDtPid database failure.", e);
		}
	}
	//查询所有商品
	@SuppressWarnings("unchecked")
	@Override
	public List<DoingProduct> getProductAll(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.guoguojie.persistence.DoingMapper.getProductAll", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getProductAll database failure.", e);
			}
			throw new AppBusinessException("getProductAll database failure.", e);
		}
	}

}
